package com.cat.admin.handler;

import com.cat.common.exception.BaseException;
import com.cat.common.exception.TokenException;
import com.cat.common.model.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理
 *
 * @author dd-cat
 * @version 1.0
 * @date 2020-11-25 13:33
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
    /**
     * 基础异常
     */
    @ExceptionHandler
    public Result<?> baseException(BaseException e) {
        log.error("程序异常：" + e.getMessage(), e);
        return Result.fail(e.getMessage());
    }

    /**
     * Token异常
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public Result<?> businessException(TokenException e) {
        log.error("程序异常：" + e.getMessage(), e);
        return Result.fail(HttpStatus.UNAUTHORIZED.value(), e.getMessage());
    }

    /**
     * 全局异常
     *
     * @param e 异常
     * @return Result
     */
    @ExceptionHandler
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public Result<?> basicExceptionHandling(Exception e) {
        log.error("程序异常：" + e.getMessage(), e);
        return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
    }
}
